<?php
require_once "../include/gallery/galleryBuilder.php";
require_once "../include/connectionHandlers/database/Image.php";
require_once "../include/connectionHandlers/database/Description.php";

class describe_controller
{
	private $photoArrayIds = [];

	public function __construct()
		{
			$this->displayGallery();
			if(isset($_POST["saveDescription"]))
			{
				$this->saveDescription();
			}
		}
	
	/**
	* Display the gallery of Images in Database with a Title and description text box
	**/
	public function displayGallery()
	{
		//Load Images from database
		$image = new Image();
		$photoArray = $image->selectAllImageUrls();
		
		//Load Ids of the images displayed
		foreach($photoArray as $key=>$value)
		{
			$this->photoArrayIds[] = $key;
		}
		
		//Display the Gallery
		$galleryBuilder = new galleryBuilder("",$photoArray,"LARGER");
		$galleryBuilder->printGalleryInColumn();
		return;
	}
	
	/**
	* Save the description (title and text) entered
	*  $_POST["desciptionText"] &  $_POST["desciptionTitle"]
	**/
	private function saveDescription()
	{
		foreach($this->photoArrayIds as $value)
		{
			
			$description = "desciptionText" . $value;
			$title = "desciptionTitle" . $value;
			
			if (isset($_POST[$description]))
				{$descriptionContent = $_POST[$description];}
			if (isset($_POST[$title]))
				{$titleContent = $_POST[$title];}
			
			$parameters = [$value,$titleContent,$descriptionContent];
			
			$description = new Description();
			$description->insert($parameters);			
		}
	}
}
?>